package com.zkk.filter;

import com.zkk.pojo.Manage;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebFilter("/login")
public class LoginServlet implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        try {
            Map<String, String[]> parameterMap = request.getParameterMap();
            Manage manage = new Manage();
            BeanUtils.populate(manage,parameterMap);
            if (!(manage.getUsername().equals("张柯柯") & manage.getPassword().equals("123"))) {
                response.sendRedirect("/login.jsp");
            }else if("".equals(manage.getUsername()) && "".equals(manage.getPassword())){
                response.sendRedirect("/login.jsp");
            }else {
                filterChain.doFilter(request, response);
            }
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }



    }
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
    }

    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}

